
#include "gamewindowlabel.h"


GameWindowLabel::GameWindowLabel(QWidget*parent):QLabel(parent)
{
    // 开启背景设置
    this->setAutoFillBackground(true);
    // 创建调色板对象
    QPalette p = this->palette();
    // 加载图片
    QPixmap pix(":/beijing.jpg");
    // 设置图片
    p.setBrush(QPalette::Window, QBrush(pix));
    this->setPalette(p);
//添加背景图案

    level = 1;
    score = 0;
    lockedWord = NULL;

    cannonLen = 150;
 //   setStyleSheet("background-color:gray");//上色用
    effect = new QSoundEffect(this);
    effect->setSource(QUrl("qrc:///touxi.wav"));
    effect->setLoopCount(1);//循环次数
    effect->setVolume(0.25f);//音量

   // setStyleSheet("background-color:#87CEEB");//背景颜色


    timerCannon = new QTimer(this);
    timerCannon->setSingleShot(true);
    connect(timerCannon,&QTimer::timeout,this,[=](){

        cannonLen = 150;
        update();
                                                       });  //后坐力实现


    timerRunning = new QTimer(this);
    connect(timerRunning,&QTimer::timeout,this,[=](){
        for(int i = 0;i<wordList.size();i++){
            enemyword *word = wordList[i];
            word->setPos(QPoint(word->getPos().x(),word->getPos().y()+1));
         word->ballsApproach();

        }//实现单词格子的下落
        for(int i = 0;i<wordList.size();i++){
         enemyword *word = wordList[i];
         if(word -> getDestroyed() == true && word -> getBallList()->size()==0){
             wordList.removeAt(i);
             score += word->getWord().size();
             delete word;

             continue;

         }
         if(word -> getDestroyed() == false && word->getPos().y() > gameDeadLine){
             if(lockedWord == word){
                 lockedWord = NULL;

             }
             wordList.removeAt(i);
             delete word;




        }



        update();

        }
    });
    timerRunning->start(20);
}



void GameWindowLabel::paintEvent(QPaintEvent *e){
    Q_UNUSED(e);
    QPainter painter(this);
   // painter.drawLine(0,0,800,800);


    QRect rect = this ->rect();


    QRect scoreRect;
    QRect levelRect;

    levelRect.setTop(rect.bottom() - 80);
    levelRect.setBottom(rect.bottom() - 30);
    levelRect.setLeft(50);
    levelRect.setRight(50 + rect.width() / 5);



    scoreRect.setTop(rect.bottom() - 80);
    scoreRect.setBottom(rect.bottom() - 30);
    scoreRect.setRight(rect.right() - 50);
    scoreRect.setLeft(rect.right() - 50 - rect.width() / 5);


    QRect fireRect;

    fireRect.setTop(rect.bottom() - 100);
    fireRect.setBottom(rect.bottom() + 100);
    fireRect.setRight(rect.width() / 2 - 100);
    fireRect.setLeft(rect.width() / 2 + 100);
    firePoint = fireRect.center();

    painter.drawRect(fireRect);

    QPen pen = painter.pen();
    pen.setWidth(3);
    painter.setPen(pen);

    gameDeadLine = fireRect.top() - 20;

    painter.setBrush(Qt::gray);
   // painter.drawRect(0,gameDeadLine,rect.width(),500);
   // painter.drawLine(0,gameDeadLine,rect.right(),gameDeadLine);



   // painter.drawLine(firePoint,targetPoint);
    QFont font = painter.font();
    font.setFamily("黑体");
    font.setPixelSize(30);
    font.setBold(true);
    painter.setFont(font);
    painter.drawRoundedRect(scoreRect,20,20);
    painter.drawRoundedRect(levelRect,20,20);
    painter.drawText(scoreRect,Qt::AlignLeft | Qt::AlignVCenter,"  得分：" + QString::number(score));



    for(int i = 0;i<wordList.size();i++){
        enemyword *word = wordList[i];

        QList<cannonball *> *ballList = word->getBallList();
        for(int j = 0;j<ballList->size();j++){
            QRect r;
            r.setTop((*ballList)[j]->getCurPos().y() - 10);
            r.setBottom((*ballList)[j]->getCurPos().y() + 10);
            r.setLeft((*ballList)[j]->getCurPos().x() - 10);
            r.setRight((*ballList)[j]->getCurPos().x() + 10);
            painter.drawEllipse(r);
    }

    pen.setColor(Qt::lightGray);
    painter.setPen(pen);

    for(int i = 0;i<wordList.size();i++){
        enemyword *word = wordList[i];

        QRect wordRect;
        if(word->getDestroyed() == false){
            continue;

        }//消灭单词后变成的样子

        wordRect.setTop(word->getPos().y() - word->getHalfH());
        wordRect.setBottom(word->getPos().y() + word->getHalfH());
        wordRect.setLeft(word->getPos().x() - word->getHalfW());
        wordRect.setRight(word->getPos().x() + word->getHalfW());
        painter.drawRect(wordRect);
        QString wordStr = word->getWord();
        painter.drawText(wordRect,Qt::AlignCenter,wordStr);

    }
    pen.setColor(Qt::black);
    painter.setPen(pen);
    for(int i = 0;i<wordList.size();i++){
        enemyword *word = wordList[i];

        QRect wordRect;
        if(word->getDestroyed() == true){
            continue;}

        wordRect.setTop(word->getPos().y() - word->getHalfH());
        wordRect.setBottom(word->getPos().y() + word->getHalfH());
        wordRect.setLeft(word->getPos().x() - word->getHalfW());
        wordRect.setRight(word->getPos().x() + word->getHalfW());
        painter.drawRoundedRect(wordRect,10,10);
        QString wordStr = word->getWord();
        painter.drawText(wordRect,Qt::AlignCenter,wordStr);
        if(word == lockedWord){
            int curIndex = word->getCureIndex();
            if(curIndex){
                pen.setColor(Qt::red);
                painter.setPen(pen);
                QString s =wordStr.mid(0,curIndex);
                for(int j = curIndex;j<wordStr.size();j++){
                    s+=" ";

                }
                painter.drawText(wordRect,Qt::AlignCenter,s);
                pen.setColor(Qt::black);
                painter.setPen(pen);
            }
        }




        }
    }


    double angle = atan(1.0 * (targetPoint.x() - firePoint.x()) / (firePoint.y() - targetPoint.y()));

    painter.save();
    painter.translate(firePoint);
    painter.rotate(angle * 180 / 3.1415926535);
    QRect rectCannon;
    rectCannon.setTop(0 - cannonLen);
    rectCannon.setBottom(0 + 200);
    rectCannon.setRight(0 - 15);
    rectCannon.setLeft(0 + 15);

    painter.drawRect(rectCannon);
    painter.restore();


    painter.setBrush(Qt::gray);
    painter.drawEllipse(fireRect);
    QPixmap pixmap(":/baoguo2.jpg");
    //painter.drawPixmap(100,100,100,100,QPixmap(":/baoguo2.jpg"));
    painter.drawPixmap(QRect(350,675,100,100),pixmap,QRect(100,20,300,300));//坦克马保国


}//实现画东西的操作


void GameWindowLabel::mouseMoveEvent(QMouseEvent *ev) {
    qDebug() << ev->x() << ev->y();
    targetPoint.setX(ev->x());
    targetPoint.setY(ev->y());
 //   for(int i = 0;i<wordList.size();i++){
   //     enemyword *word = wordList[i];
  //      word->setPos(QPoint(ev->x(),ev->y()));

  //  }


    update();
}





void GameWindowLabel::getKey(const QString &c){
    if(c == "")
        return;
    cannonLen = 130;
    update();
    effect->play();

    timerCannon->start(150);

    if(lockedWord == NULL){
        //找一个最佳单词并锁定
        int h_min = 0;
        enemyword *lword = NULL;
        for(int i = 0;i<wordList.size();i++){
            enemyword *word = wordList[i];
            int y = word->getPos().y();
            if(word->getDestroyed() == false && word->getWord()[0] == c && y > h_min){
                h_min = y;
                lword = word;

        }

        }

        lockedWord = lword;
    }
    if(lockedWord ){
        targetPoint = lockedWord->getPos();
    if(false == lockedWord->appendBall(c)){
        lockedWord = NULL;

    }

    }


    qDebug()<<c;

}


void GameWindowLabel::generateWords(int count){
    srand(QTime::currentTime().msec());
    for(int i=0;i<count ;i++){
    int w = rand() % width();
    int h = rand() % height() - height();
    enemyword *word = new enemyword("hello",firePoint,QPoint(w,h));
    if(word->getPos().x() - word->getHalfW() < 0 || word->getPos().x() + word->getHalfW() > width()){
        delete word;
        i--;

    }

    else{
        wordList.append(word);}


    }

}
